package com.termux.tasker.utils;

import ohos.app.Context;
import com.termux.tasker.Constants;
import ohos.bundle.IBundleManager;
import ohos.bundle.ApplicationInfo;
import ohos.rpc.RemoteException;

public class PluginUtils {

    /**
     * Determines the "versionCode" in the {@code ohos}.
     *
     * @param context to read the versionCode.
     * @return versionCode of the app.
     */

    public static int getVersionCode(final Context context) {
        if (null == context) {
            throw new IllegalArgumentException("context cannot be null"); //$NON-NLS-1$
        }

        try {
            return context.getBundleManager().getBundleInfo(context.getBundleName(), 0).getVersionCode();
        } catch (final UnsupportedOperationException e) {
            // This exception is thrown by test contexts.
            return 1;
        } catch (final Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Check if package has { com.termux.tasker.Constants#PERMISSION_RUN_COMMAND}.
     *
     * @param context     to get error string.
     * @param packageName to check.
     * @return errmsg if package has not been granted
     * { com.termux.tasker.Constants#PERMISSION_RUN_COMMAND}, otherwise null.
     */
    public static String checkIfPackageHasPermissionRunCommand(final Context context, final String packageName) throws RemoteException {

        String errmsg = null;

        // Check if packageName has been granted PERMISSION_RUN_COMMAND
        IBundleManager packageManager = context.getBundleManager();
        // If permission not granted
        if (packageManager.checkPermission(Constants.PERMISSION_RUN_COMMAND, packageName) != IBundleManager.PERMISSION_GRANTED) {
            ApplicationInfo applicationInfo;
            try {
                applicationInfo = packageManager.getApplicationInfo(packageName, 0, 0);
            } catch (final RemoteException e) {
                applicationInfo = null;
            }
            final String appName = (String) (applicationInfo != null ? packageManager.getApplicationInfo(String.valueOf(applicationInfo), 0, 0) : "(unknown)");
            errmsg = String.format("The %1$s App (%2$s) has not been granted the \\\"%3$s\\\" permission which is required to run the plugin action.\"", appName, packageName, Constants.PERMISSION_RUN_COMMAND);
        }

        return errmsg;
    }

    /**
     * Check if executable is not in {@link Constants#TASKER_PATH} and
     * {@link Constants#ALLOW_EXTERNAL_APPS_PROPERTY} property is not set to "true".
     *
     * @param context    to get error string.
     * @param executable path to check.
     * @return errmsg if policy is violated, otherwise null.
     */
    public static String checkIfAllowExternalAppsPolicyIsViolated(final Context context, final String executable) {
        if (executable == null || executable.isEmpty()) { return "The executable is null or empty."; }

        String errmsg = null;

        if (!FileUtils.isPathInTaskerDir(executable) && !TermuxAppUtils.isAllowExternalApps(context)) {
            errmsg = "Absolute paths for executables outside ~/.termux/tasker/ directory require allow-external-apps property to be set to \\\"true\\\" in ~/.termux/termux.properties file.";
        }

        return errmsg;
    }
}
